<template>
<div class="bg" :style="{'background-image':`url(${require('@/images/login.jpg')})`}"> 
  <div class="box">
    <nav>
      <router-link to="/login">登录</router-link>
      <h2>·</h2>
      <router-link to="/register">注册</router-link>
    </nav>
    <div>
      <el-form ref="regForm" :model="formData" label-width="0" status-icon :rules="myrules">
        <el-form-item label prop="regUserName">
          <el-input
            v-model="formData.regUserName"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>

        <el-form-item label prop="regPassword">
          <el-input
            v-model="formData.regPassword"
            placeholder="请输入密码"
            type="password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item label prop="checkPassword">
          <el-input
            v-model="formData.checkPassword"
            placeholder="请输入密码"
            type="password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="success" class="btn" round @click="fn('regForm')">立即注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</div>
</template>

<script>
export default {
  data() {
   
    var validateCheckPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.formData.regPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
        
      formData: {
        regUserName: "",
        regPassword: "",
        checkPassword: "",
      },
      
      myrules: {
        regUserName: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
          { min: 3, max: 15, message: "必须3-15位", trigger: "blur" },

        ],
        regPassword: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 3, max: 12, message: "必须3-12位", trigger: "blur" },
        ],
        checkPassword: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { validator: validateCheckPass, trigger: "blur" },
        ],
      },

    };
  },

  methods: {
    fn(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
                   console.log("可以提交了")
          // 获取缓存中已经保存的用户列表
          const userList = window.localStorage.getItem('userList') ? JSON.parse(window.localStorage.getItem('userList')) : [] 
          userList.push({
            name: this.formData.regUserName,
            password: this.formData.regPassword
          })
          // 保存注册的用户数据到缓存中
          window.localStorage.setItem('userList', JSON.stringify(userList))

          this.$message.success('注册成功')
          this.$router.push('/login')

        } else {
          console.log("不能提交");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.bg{
	
	background-size: 100% 100%;
	width: 100%;
	height: 100%;
	position: fixed;
    background-repeat: no-repeat;
}
.box {
  width: 380px;
  height: 430px;
  background: white;
  box-shadow: 0 0 5px 3px #ddd;
  border: 1px solid #ccc;
  margin: 60px auto 0;
  padding: 40px;
  text-align: center;
 
}
.bg>>> .el-input__icon{
    padding-bottom: 15px;
    height: 100%;
    width: 25px;
    text-align: center;
    transition: all .3s;
    line-height: 40px;
    padding-bottom: 15px;
}
h2 {
  display: inline;
  margin: 0 6px;
}
nav a {
  text-decoration: none;
  font-size: 20px;
  color: black;
}
nav a.router-link-active {
  color: red;
  text-decoration: underline;
}
.box nav {
  margin-bottom: 30px;
}
.btn {
  width: 100%;
}
</style>